using System;
using System.Xml;
using System.Runtime.Serialization;

namespace Microsoft.MSE.Catalog.Module.Configuration
{
    /// <summary>
    /// 
    /// </summary>
    [DataContract(Name = "WCFCatalogSvcConfig", Namespace = MseConstants.MSENamespace)]
    public class CatalogSvcConfig
    {
        private string _connectionString;

        [DataMember(Order = 0)] public bool DebugMode;

        /// <summary>
        /// Note: I want to start absracting settings a little better
        /// </summary>
        [DataMember(Order = 1)] public string DBConnString
        {
            get
            {
                // get setting
                string connectionString = SettingsManager.Repository.ConnectionString;
                
                // right now there isn't a backing store so if there isn't a
                // value that means we didn't have arguments passed in from the 
                // command line in that case return the value from the config.
                if (!string.IsNullOrEmpty(connectionString))
                {
                    return connectionString;     
                }
  
                return _connectionString;
            }

            set
            {
                _connectionString = value;
            }
        }

        [DataMember(Order = 2)] public Uri baseAddress;
        [DataMember(Order = 3)] public ServiceEndpointElement[] serviceEndpoints;
        [DataMember(Order = 4)] public ServiceCatalogThrottle serviceCatalogThrottle;
        [DataMember(Order = 5, IsRequired = false)] public ServiceCatalogUddi serviceCatalogUddi;
        [DataMember(Order = 6, IsRequired = false)] public int StartupRetryInterval;
        [DataMember(Order = 7, IsRequired = false)] public int StartupRetryAttempts;
        [DataMember(Order = 8, IsRequired = false)] public ChangeMonitorElement RepositoryChangeMonitor;
    }

    [DataContract(Name = "WCFCatalogSvcEndpointElement", Namespace = MseConstants.MSENamespace)]
    public class ServiceEndpointElement
    {
        [DataMember(Order = 0)] public Uri address;
        [DataMember(Order = 1)] public XmlElement bindingXml;
    }

    [DataContract(Name = "ServiceCatalogThrottle", Namespace = MseConstants.MSENamespace)]
    public class ServiceCatalogThrottle
    {
        [DataMember(Order = 0)] public int MaxConcurrentCalls;
        [DataMember(Order = 1)] public int MaxConcurrentSessions;
        [DataMember(Order = 2)] public int MaxConcurrentInstances;
    }

    [DataContract(Name = "ServiceCatalogUddi", Namespace = MseConstants.MSENamespace)]
    public class ServiceCatalogUddi
    {
        [DataMember(Order = 0, IsRequired = true)] public bool UddiIntegrationMode;
        [DataMember(Order = 1)] public string UddiPublishUrl;
        [DataMember(Order = 2)] public string UddiInquireUrl;
        [DataMember(Order = 3)] public string UddiUserName;
        [DataMember(Order = 4)] public string UddiPassword;
        [DataMember(Order = 5)] public string BusinessKey;
        [DataMember(Order = 6)] public AuthnticationScheme UddiAuthenticationScheme;
    }

    [DataContract(Name = "AuthnticationScheme", Namespace = MseConstants.MSENamespace)]
    public enum AuthnticationScheme
    {
        [EnumMember] UDDI,
        [EnumMember] WINDOWS
    }

    [DataContract(Name = "RepositoryChangeMonitor", Namespace = MseConstants.MSENamespace)]
    public class ChangeMonitorElement
    {
        [DataMember(Order = 0, IsRequired = true)] public bool Enabled;
        [DataMember(Order = 1, IsRequired = true)] public int PollingInterval;
        [DataMember(Order = 2, IsRequired = true)] public int MessageExpiration;
    }
}